package com.ruoyi.wms.service;

import com.ruoyi.app.domain.entity.AppMailboxMessage;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.bo.SysUserBo;
import com.ruoyi.system.domain.vo.SysUserVo;
import com.ruoyi.wms.domain.entity.MailboxAppUser;
import com.ruoyi.wms.domain.vo.MailboxStatisticsVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.domain.bo.MailboxMessageBo;
import com.ruoyi.wms.domain.vo.MailboxMessageVo;
import com.ruoyi.wms.domain.entity.MailboxMessage;
import com.ruoyi.wms.mapper.MailboxMessageMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 信件管理Service业务层处理
 *
 * @author zcc
 * @date 2024-12-14
 */
@RequiredArgsConstructor
@Service
public class MailboxMessageService {

    private final MailboxMessageMapper mailboxMessageMapper;

    public int today_quantity(){
        return mailboxMessageMapper.today_quantity();
    }
    public int week_qunatity(){
        return mailboxMessageMapper.week_qunatity();
    }

    public int month_quantity(){
        return mailboxMessageMapper.month_quantity();
    }
    public int total(){
        return mailboxMessageMapper.total();
    }
    public int processed_total(){
        return mailboxMessageMapper.processed_total();
    }
    public int pending_total(){
        return mailboxMessageMapper.pending_total();
    }
    public int today_processed_total(){
        return mailboxMessageMapper.today_processed_total();
    }


    /**
     * 查询信件管理
     */
    public MailboxMessageVo queryById(Long id){
        return mailboxMessageMapper.selectVoById(id);
    }


    public TableDataInfo<MailboxMessageVo> queryPageMessageList(MailboxMessageBo bo, PageQuery pageQuery) {
        Page<MailboxMessageVo> page = mailboxMessageMapper.queryPageMessageList(pageQuery.build(), this.buildQueryWrapper(bo));
        return TableDataInfo.build(page);
    }
    /**
     * 查询信件管理列表
     */
    public TableDataInfo<MailboxMessageVo> queryPageList(MailboxMessageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MailboxMessage> lqw = buildQueryWrapper(bo);
        Page<MailboxMessageVo> result = mailboxMessageMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询信件管理列表
     */
    public List<MailboxMessageVo> queryList(MailboxMessageBo bo) {
        LambdaQueryWrapper<MailboxMessage> lqw = buildQueryWrapper(bo);
        return mailboxMessageMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MailboxMessage> buildQueryWrapper(MailboxMessageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MailboxMessage> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), MailboxMessage::getName, bo.getName());
        lqw.like(bo.getId()!=null, MailboxMessage::getId, bo.getId());
        lqw.eq(bo.getAuthorId() != null, MailboxMessage::getAuthorId, bo.getAuthorId());
        lqw.eq(bo.getGender() != null, MailboxMessage::getGender, bo.getGender());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), MailboxMessage::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getAddressValue()), MailboxMessage::getAddressValue, bo.getAddressValue());
        lqw.eq(StringUtils.isNotBlank(bo.getAddressIds()), MailboxMessage::getAddressIds, bo.getAddressIds());
        lqw.eq(StringUtils.isNotBlank(bo.getDomicileAddress()), MailboxMessage::getDomicileAddress, bo.getDomicileAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), MailboxMessage::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), MailboxMessage::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getTheme()), MailboxMessage::getTheme, bo.getTheme());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), MailboxMessage::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), MailboxMessage::getImage, bo.getImage());
        lqw.eq(StringUtils.isNotBlank(bo.getVideo()), MailboxMessage::getVideo, bo.getVideo());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), MailboxMessage::getStatus, bo.getStatus());
        lqw.eq(bo.getUserId() != null, MailboxMessage::getUserId, bo.getUserId());
        lqw.eq(bo.getDeptId() != null, MailboxMessage::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), MailboxMessage::getDeptName, bo.getDeptName());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptEnclosureImage()), MailboxMessage::getDeptEnclosureImage, bo.getDeptEnclosureImage());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptEnclosureVideo()), MailboxMessage::getDeptEnclosureVideo, bo.getDeptEnclosureVideo());
        lqw.eq(bo.getReply() != null, MailboxMessage::getReply, bo.getReply());
        lqw.like(StringUtils.isNotBlank(bo.getReplyName()), MailboxMessage::getReplyName, bo.getReplyName());
        lqw.between(params.get("beginTime") != null && params.get("endTime") != null, MailboxMessage::getCreateTime
            , params.get("beginTime"), params.get("endTime"));
        lqw.orderByDesc(MailboxMessage::getCreateTime);
        return lqw;
    }

    /**
     * 新增信件管理
     */
    public void insertByBo(MailboxMessageBo bo) {
        MailboxMessage add = MapstructUtils.convert(bo, MailboxMessage.class);
        mailboxMessageMapper.insert(add);
    }

    /**
     * 修改信件管理
     */
    public void updateByBo(MailboxMessageBo bo) {
        MailboxMessage update = MapstructUtils.convert(bo, MailboxMessage.class);
        mailboxMessageMapper.updateById(update);
    }

    /**
     * 批量删除信件管理
     */
    public void deleteByIds(Collection<Long> ids) {
        mailboxMessageMapper.deleteBatchIds(ids);
    }
}
